package com.jzy.gulimall.product.controller;

import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.product.entity.SkuInfoEntity;
import com.jzy.gulimall.product.service.SkuInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * sku信息
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-11 10:08:13
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;


    /**
     * 订单确认页
     * 获取用户所有选中购物项
     * 获取商品最新价格
     */
    @GetMapping("/getPrice/{skuId}")
    public R getPrice(@PathVariable("skuId")Long skuId){
        SkuInfoEntity byId = skuInfoService.getById(skuId);
        return R.ok().setData(byId.getPrice().toString());
    }

    /**
     * 商品管理sku 列表
     * page=1&limit=10&key=&catelogId=0&brandId=0&min=0&max=0
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 购物车 查询sku信息
     * 远程调用 完成采购 生成库存信息 冗余skuName 信息
     * 远程调用 秒杀 查询sku信息
     */
    @RequestMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 发布商品 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
